from django.shortcuts import render
from django.views.decorators.http import require_http_methods
from django.core import serializers
from django.http import JsonResponse
import json
from myapp.utils.road_loadjson import road_loadjson
from myapp.utils.genderToJson import genderToJson
from myapp.utils.identifiedToJson import identifiedToJson
from myapp.utils.explosion_loadjson import explosion_loadjson
from myapp.utils.ageToJson import ageToJson
from myapp.utils.heatToJson import heatToJson
from myapp.utils.terminalToJson import terminalToJson
from myapp.utils.word_mapTojson import word_cloud_json_data, map_cloud_json_data
from myapp.utils.detail_loadjson import detail_loadjson
from myapp.utils.locationToJson import locationToJson
from myapp.utils.participant_loadjson import participant_num


@require_http_methods(["GET"])
def load_road(request):
    response = {}
    try:
        key = request.GET.get('key')
        mid = request.GET.get('mid', '')
        response = road_loadjson(key, mid)
    except Exception as e:
        print(e.with_traceback())
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)


@require_http_methods(["GET"])
def load_gendar(request):
    response = {}
    try:
        key = request.GET.get('key')
        mid = request.GET.get('mid', '')
        response = genderToJson(key, mid)
    except Exception as e:
        print(e)
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)


@require_http_methods(["GET"])
def load_identify(request):
    response = {}
    try:
        key = request.GET.get('key')
        mid = request.GET.get('mid', '')
        response = identifiedToJson(key, mid)
    except Exception as e:
        print(e)
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)


@require_http_methods(["GET"])
def load_explosion(request):
    response = {}
    try:
        key = request.GET.get('key')
        mid = request.GET.get('mid', '')
        response = explosion_loadjson(key, mid)
    except Exception as e:
        print(e)
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)


@require_http_methods(["GET"])
def load_age(request):
    response = {}
    try:
        key = request.GET.get('key')
        mid = request.GET.get('mid', '')
        response = ageToJson(key, mid)
    except Exception as e:
        print(e)
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)


@require_http_methods(["GET"])
def load_heat(request):
    response = {}
    try:
        key = request.GET.get('key')
        response = heatToJson(key)
    except Exception as e:
        print(e)
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)


@require_http_methods(["GET"])
def load_wordcloud(request):
    response = {}
    try:
        key = request.GET.get('key')
        response = word_cloud_json_data(key)
    except Exception as e:
        print(e)
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)


@require_http_methods(["GET"])
def load_map(request):
    response = {}
    try:
        key = request.GET.get('key')
        mid = request.GET.get('mid', '')
        response = locationToJson(key, mid)
    except Exception as e:
        print(e)
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)


@require_http_methods(["GET"])
def load_terminal(request):
    response = {}
    try:
        key = request.GET.get('key')
        mid = request.GET.get('mid', '')
        response = terminalToJson(key, mid)
    except Exception as e:
        print(e)
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)


@require_http_methods(["GET"])
def load_detail(request):
    response = {}
    try:
        key = request.GET.get('key')
        mid = request.GET.get('mid', '')
        response = detail_loadjson(key, mid)
    except Exception as e:
        print(e)
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)


@require_http_methods(["GET"])
def load_participant(request):
    response = {}
    try:
        key = request.GET.get('key')
        response = participant_num(key)
    except Exception as e:
        print(e)
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)
